import { connect } from 'dva'

import AekLinkButton from '@/components/AekLinkButton'
import AekTable from '@/components/AekTable'
import AekUpload from '@/components/AekUpload'
import PageLayout from '@/components/PageLayout'
import SearchForm from '@/components/SearchForm'
import { namespace } from '@/models/pcs/insurance'
import { getPagination, message } from '@/utils/antdHelper'
import { getBasicFn } from '@/utils/dvaUtil/index'
import { GENDER_ENUM, PCS_TRANSACTION_STATUS_ENUM } from '@/utils/enum'

import HandleModal from './components/HandleModal'

/** 陪护 - 长护险管理 */
const Index = ({ modelState, loading }) => {
  const { pageList, pagination, searchParams } = modelState
  const { toAction, getLoading } = getBasicFn({ namespace, loading })

  const handle = async ({ row, view }) => {
    await HandleModal.PromiseThen({
      row,
      view,
    })
    message.success('办理成功')
    toAction('getPageList')
  }

  /** @type {import("@/components/SearchForm").SearchFormProps} */
  const searchProps = {
    searchParams,
    formData: [
      {
        field: 'status',
        label: '状态',
        component: {
          name: 'Select',
          props: {
            options: PCS_TRANSACTION_STATUS_ENUM.options,
            defaultValue: PCS_TRANSACTION_STATUS_ENUM.ALL,
          },
        },
      },
      {
        field: 'name',
        label: '姓名',
        component: {
          name: 'Input',
        },
      },
      {
        field: 'idNumber',
        label: '身份证号',
        component: {
          name: 'Input',
        },
      },
      {
        field: 'phone',
        label: '联系方式',
        component: {
          name: 'Input',
        },
      },
    ],
    onSearch: values => {
      toAction({ searchParams: values })
      toAction({ pagination: { current: 1 } }, true)
      toAction('getPageList')
    },
  }

  /** @type {import("@/components/AekTable").AekTableProps} */
  const tableProps = {
    configKey: namespace,
    rowKey: 'id',
    loading: getLoading('getPageList'),
    dataSource: pageList,
    pagination: getPagination(pagination, (current, pageSize) => {
      toAction('getPageList', { current, pageSize })
    }),
    columns: [
      { dataIndex: 'applyNumber', title: '申请单号' },
      { dataIndex: 'status', title: '状态', enum: PCS_TRANSACTION_STATUS_ENUM },
      { dataIndex: 'name', title: '姓名' },
      { dataIndex: 'idNumber', title: '身份证号' },
      { dataIndex: 'gender', title: '性别', enum: GENDER_ENUM },
      { dataIndex: 'phone', title: '联系电话' },
      { dataIndex: 'address', title: '详细地址', ellipsis: true },
      {
        dataIndex: 'proveFile',
        title: '申请证明',
        render: text =>
          text && JSON.parse(text).length === 2 ? (
            <div className='h-17'>
              <AekUpload
                limitFileNum={2}
                value={[
                  {
                    name: '身份证正面',
                    url: JSON.parse(text)[0].url,
                  },
                  {
                    name: '身份证反面',
                    url: JSON.parse(text)[1].url,
                  },
                ]}
                showUploadList={{
                  showRemoveIcon: false,
                }}
              />
            </div>
          ) : (
            '-'
          ),
      },
      { dataIndex: 'remark', title: '备注', ellipsis: true },
      { dataIndex: 'createTime', title: '申请时间', isDateTime: true },
      { dataIndex: 'handler', title: '办理人' },
      { dataIndex: 'handlingTime', title: '办理时间', isDateTime: true },
      {
        key: 'operate',
        title: '操作',
        fixed: 'right',
        width: 100,
        className: 'text-center!',
        render: (_, row) => (
          <AekLinkButton onClick={() => handle({ row, view: row.status === PCS_TRANSACTION_STATUS_ENUM.PROCESSED })}>
            {row.status === PCS_TRANSACTION_STATUS_ENUM.PROCESSED ? '查看' : '办理'}
          </AekLinkButton>
        ),
      },
    ],
  }

  return (
    <PageLayout>
      <SearchForm {...searchProps} />
      <AekTable {...tableProps} />
    </PageLayout>
  )
}

export default connect(store => ({
  modelState: store[namespace],
  loading: store.loading,
}))(Index)
